package com.sunline.poc.oozie.dao.impl;

import com.sunline.poc.oozie.config.UserDefinesConfig;
import com.sunline.poc.oozie.dao.UserDao;
import com.sunline.poc.oozie.vo.OozieJobConfigVO;
import com.sunline.poc.oozie.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    UserDefinesConfig userDefinesConfig;

    @Override
    public UserInfoVO findUserByUsername(String userName,String passWord) {
        return findUser(userName,passWord);
    }

    private UserInfoVO findUser(String uname,String pswd){
        List<Map<String,String>> configList =  userDefinesConfig.getDefines();
        for (Map<String,String> config:configList) {
            if (uname.equalsIgnoreCase(config.get("userName")) && pswd.equalsIgnoreCase(config.get("passWord"))){
                return genUserInfo(config);
            }
        }
        return  null;
    }

    private UserInfoVO genUserInfo(Map<String,String> config){
        UserInfoVO userInfoVO = new UserInfoVO();
        Iterator<Map.Entry<String, String>> entries = config.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            if ("userName".equalsIgnoreCase(entry.getKey())){
                userInfoVO.setUserName(entry.getValue());
            }
            if ("passWord".equalsIgnoreCase(entry.getKey())){
                userInfoVO.setPassWord(entry.getValue());
            }
            if ("kerberosPrincipal".equalsIgnoreCase(entry.getKey())){
                userInfoVO.setKerberosPrincipal(entry.getValue());
            }
            if ("kerberosKeytabFilePath".equalsIgnoreCase(entry.getKey())){
                userInfoVO.setKerberosKeytabFilePath(entry.getValue());
            }
        }
        return userInfoVO;
    }

//    private UserInfoVO genCliantA(){
//        UserInfoVO client = new UserInfoVO("clientA");
//        client.setKerberosPrincipal("clientA@DC.COM");
//        client.setKerberosKeytabFilePath("D:\\Temp\\clientA.keytab");
//        client.setPassWord("123456");
//        return client;
//    }
//
//
//    private UserInfoVO genClientB(){
//        UserInfoVO client = new UserInfoVO("clientA");
//        client.setKerberosPrincipal("clientA@DC.COM");
//        client.setKerberosKeytabFilePath("D:\\Temp\\clientA.keytab");
//        client.setPassWord("123456");
//        return  client;
//    }
}
